<?php

namespace App\Http\Requests;


use App\Services\ApprovalService;
use App\Services\OrganizeService;
use Illuminate\Validation\Rule;

class ApprovalSetRequest extends BaseRequest
{
    /**
     * 这个地方记得为 true, 默认为 false 会返回 403 未授权提示
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * 验证规则
     * @return string[]
     */
    public function rules()
    {
        $types = self::getTypes();
        return [
            'user_id' => 'required|exists:users,id',
            'level'   => ['required', Rule::in($types)], //最大审批级别为 4
            'type'    => 'required|integer'
        ];

    }

    /**
     * 验证不通过提示
     * @return string[]
     */
    public function messages()
    {
        return [
            'user_id.required' => '请指定用户',
            'user_id.exists'   => '用户不存在',
            'level.required'   => '请指定审核级别',
            'level.in'         => '审核级别参数非法',
            'type.required'    => '审批类型必填',
            'type.integer'     => '审批类型值非法',
        ];
    }

    /**
     * 获取类型ids
     * @return array
     */
    private static function getTypes()
    {
        $types = ApprovalService::getType();

        return array_column($types, 'type');
    }


}
